package cn.zhaozhiqi.article.service.impl;

import cn.zhaozhiqi.article.domain.Article;
import cn.zhaozhiqi.article.mapper.ArticleMapper;
import cn.zhaozhiqi.article.query.ArticleQuery;
import cn.zhaozhiqi.article.service.IArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Article getByID(Long id) {
        return articleMapper.getByID(id);
    }

    @Override
    public List<Article> getAll() {
        List<Article> all = articleMapper.getAll();
        return all;
    }

    @Override
    public void addData(Article article) {
        article.setCreateTime(new Date());
        articleMapper.addData(article);
    }

    @Override
    public void updateByID(Article article) {
        article.setUpdateTime(new Date());
        articleMapper.updateByID(article);
    }

    @Override
    public void deleteByID(Long id) {
        articleMapper.deleteByID(id);
    }

    @Override
    public PageInfo<Article> articleLimit(ArticleQuery query) {
        // 1.下面这句代码表示开启分页，会将紧挨着这句代码的下一次数据库执行进行分页，切记一定要放在要分页的SQL语句前面执行
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        List<Article> list = articleMapper.getSearchList(query);
        PageInfo<Article> articlePageInfo = new PageInfo<>(list);
        return articlePageInfo;
    }

    @Override
    public void batchdelete(List<Long> ids) {
        articleMapper.batchdelete(ids);
    }

}
